package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐服务
 */
@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService{
    //使用JedisPool操作Redis服务
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private SetmealDao setmealDao;
    //新增套餐信息,同时需要关联检查组
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        //得到新插入的套餐的id
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckGroup(setmealId,checkgroupIds);
        //将图片名称保存到集合中
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
    }

    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id查询检查组
    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    //根据套餐id查询当前套餐包含的检查组id
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        return setmealDao.findCheckGroupIdsBySetmealId(id);
    }

    //编辑套餐同时关联检查组
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //修改基本信息
        setmealDao.edit(setmeal);
        //,然后先把原来的关联关系清除
        setmealDao.deleteAssocication(setmeal.getId());
        //,然后再建立关联关系
        Integer SetmealId = setmeal.getId();
        this.setSetmealAndCheckGroup(SetmealId,checkgroupIds);
    }

    //删除套餐
    @Override
    public void delete(Integer id) {
        //先把原来的关联关系清除
        setmealDao.deleteAssocication(id);
        //然后删除检查组
        setmealDao.delete(id);
    }

    //查询所有套餐
    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    //查询套餐详细信息(套餐基本信息,套餐包含的检查组,检查组包含的检查项)
    @Override
    public Setmeal findByIddddddd(Integer id) {
        return setmealDao.findByIddddddd(id);
    }

    //查询套餐占比饼形图
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }

    //设置套餐和检查组多对多关系,操作t_setmeal_checkgroup表
    public void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds){
        if (checkgroupIds !=null && checkgroupIds.length>0) {
            for (Integer checkgroupId : checkgroupIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
}
